import {
	asyncRoutes,
	constantRoutes
} from '@/router'

/**
 * Use meta.role to determine if the current user has permission
 * @param roles
 * @param route
 */
function hasPermission(roles, route) {
	if (route.path) {
		// if (route.meta && route.meta.roles) {
		// return roles.some(role => route.meta.roles.includes(role))
		return roles.includes(route.path)
		// return roles.some(role => route.path.includes(role))
	} else {
		return true
	}
}

/**
 * Filter asynchronous routing tables by recursion
 * @param routes asyncRoutes
 * @param roles
 */
export function filterAsyncRoutes(routes, roles) {
	const res = []
	routes.forEach(route => {
		const tmp = {
			...route
		}
		if (hasPermission(roles, tmp)) {
			if (tmp.children) {
				tmp.children = filterAsyncRoutes(tmp.children, roles)
			}
			res.push(tmp)
		}
	})

	return res
}

function deepForAllUrl(routers, stri = '', res = []) {

	routers.forEach(item => {
		const newstr = stri ? stri + '__' + item.path : item.path;
		res.push(newstr)
		if (item.children) {
			deepForAllUrl(item.children, newstr, res)
		}
	})
	return res
}

const informationRoute = [{
	name: "核安全排名",
	id: "/RCIM-nuclearSafetyRanking"
},
{
	name: "综合数据",
	id: "/integrated-data"
},
{
	name: "视频数据",
	id: "/video-data"
},
{
	name: "历史趋势",
	id: "/RCIM-historyTrend"
},
{
	name: "异常查询",
	id: "/RCIM-abnormalSearch"
},
{
	name: "出入库记录",
	id: "/stock-record"
},
{
	name: "源状态记录",
	id: "/source-record"
},
{
	name: "GIS地理信息",
	id: "/RCIM-Geographic"
},
{
	name: "异常督办",
	id: "/RCIM-abnormalSupervision"
},
{
	name: "档案管理",
	id: "/RCIM-archives"
},
{
	name: "档案查询",
	id: "/RCIM-archivesSearch"
},
{
	name: "申报统计",
	id: "/RCIM-statistics-declaration"
}
]

const state = {
	routes: [],
	addRoutes: [],
	informationRoute: []
}

const mutations = {
	SET_ROUTES: (state, routes, realInformationRoute) => {
		state.addRoutes = routes
		state.routes = constantRoutes.concat(routes)
	},
	SET_INFORMATION_ROUTE: (state, realInformationRoute) => {
		state.informationRoute = realInformationRoute
	},
}

const actions = {
	generateRoutes({
		commit
	}, roles) {
		// debugger
		return new Promise(resolve => {
			let accessedRoutes;
			let realInformationRoute = [];
			// 判断角色是否为超级管理员账号
			if (roles.includes('admin')) {
				accessedRoutes = asyncRoutes || []
				realInformationRoute = informationRoute
			} else {
				// 筛选出正确的权限
				const aurl = deepForAllUrl(asyncRoutes)
				const realUrl = aurl.filter(itemurl => roles.find(itemRols => itemurl.indexOf(itemRols) !== -1))

				realUrl.forEach(item => {
					roles.push(...item.split('__'))
				})
				const realRoles = Array.from(new Set(roles))
				realInformationRoute = informationRoute.filter(item => realRoles.includes(item.id))
				console.log('realUrl',realRoles, realInformationRoute)
				accessedRoutes = filterAsyncRoutes(asyncRoutes, realRoles)
				// accessedRoutes = asyncRoutes || []
			}
			const first = {
				path: '/',
				redirect: '/dashboard',
				hidden: true,
				name: "/",
				meta: {
					title: '信息舱',
					icon: 'iconfont iconFill'
				}
			}
			let i = 0;
			while (accessedRoutes[i]) {
				if (accessedRoutes[i].path === '/dashboard') {
					accessedRoutes[i].meta.affix = true
					i++
				} else {
					if (accessedRoutes[i].children) {
						first.redirect = accessedRoutes[i].children[0].path
						first.name = accessedRoutes[i].children[0].path
						accessedRoutes[i].children[0].meta.affix = true
						first.meta = Object.assign({}, accessedRoutes[i].children[0].meta, {
							affix: false
						})
					} else {
						first.redirect = accessedRoutes[i].path
						first.name = accessedRoutes[i].path
						accessedRoutes[i].meta.affix = true
						first.meta = Object.assign({}, accessedRoutes[i].meta, {
							affix: false
						})
					}
					break;
				}
			}
			if (first.redirect !== '/') {
				accessedRoutes.unshift(first)
			}
			if(accessedRoutes[0].path === '/'){
				accessedRoutes[0].meta.affix = false;
			}
			commit('SET_ROUTES', accessedRoutes)
			commit('SET_INFORMATION_ROUTE', realInformationRoute)
			resolve(accessedRoutes)
		})
	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
